<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/25 0025
 * Time: 下午 3:48
 */

namespace App\Http\Service;


class ExcelTaobao
{
    static function read_data(\Closure  $callback = null){
        set_time_limit(0); //设置页面等待时间
//        ini_set('memory_limit', 0);
        $res = self::save_excel();
        if($res == false){
            return false;
        }
        $uploadfile = storage_path('excel/upload.xls');
        $inputFileType = \PHPExcel_IOFactory::identify($uploadfile);
        $reader = \PHPExcel_IOFactory::createReader($inputFileType); // 读取 excel 文档
        $PHPExcel = $reader->load($uploadfile); // 文档名称
        $objWorksheet = $PHPExcel->getActiveSheet();
        $highestRow = $objWorksheet->getHighestRow(); // 取得总行数
        $highestColumn = $objWorksheet->getHighestColumn(); // 取得总列数
        $arr = array(
            1 => 'A', 2 => 'B', 3 => 'C', 4 => 'D', 5 => 'E', 6 => 'F', 7 => 'G', 8 => 'H', 9 => 'I',
            10 => 'J', 11 => 'K', 12 => 'L',13 => 'M', 14 => 'N', 15 => 'O', 16 => 'P', 17 => 'Q', 18 => 'R',
            19 => 'S', 20 => 'T', 21 => 'U', 22 => 'V', 23 => 'W',
            24 => 'X',25 => 'Y', 26 => 'Z');
        //echo $highestRow.$highestColumn;
        // 一次读取一列
        $res = [];
        for ($row = 2; $row <= $highestRow; $row++) {
            for ($column = 0;  $arr[$column+1] != 'Y' ; $column++) {
                $val = $objWorksheet->getCellByColumnAndRow($column, $row)->getValue();
                $res[$row-2][$column] = $val;
            }
        }
        call_user_func($callback,$res);
    }

    static function save_excel(){
        $uploadfile = storage_path('excel/upload.xls');
        if ($_FILES["file"]["error"] > 0)
        {
            return false;
        }
        else
        {
            move_uploaded_file($_FILES["file"]["tmp_name"],$uploadfile);
            return true;
        }
    }
}